﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace MyClass.Communications
{
    /// <summary>
    /// Socket发送信息
    /// </summary>
    public class ClientSocket
    {
        Socket socket;
        public delegate void EventHander(object o, string res);
        /// <summary>
        /// 侦听获得的信息字符窜
        /// </summary>
        public event EventHander CallBack;

        #region 取得终端IP地址
        /// <summary>
        /// 取得终端IP地址
        /// </summary>
        /// <returns></returns>
        public  IPAddress GetServerIP()
        {
            IPHostEntry ieh = Dns.GetHostByName(Dns.GetHostName());
            return ieh.AddressList[0];
        }
        #endregion

        #region 侦听
        /// <summary>
        /// 侦听
        /// </summary>
        public void BeginListen()
        {

            IPAddress ServerIp = GetServerIP();
            IPEndPoint iep = new IPEndPoint(ServerIp, 8000);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(iep);
            //            do

            //            while(byteMessage!=null);
            while (true)
            {
                socket.Listen(1024);
                byte[] byteMessage = new byte[100];
                string msg = "";
                try
                {
                    Socket newSocket = socket.Accept();
                    newSocket.Receive(byteMessage);

                    string sTime = DateTime.Now.ToShortTimeString();
                    // string msg = sTime + ":" + "Message from:";
                    msg += Encoding.UTF32.GetString(byteMessage);
                    //this.listBox1.Items.Add(msg);
                    CallBack(this,msg);
                }
                catch (SocketException ex)
                {
                    throw ex;

                }
            }

        }
        #endregion

        #region 发送消息
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="ip">要发送的IP地址</param>
        /// <param name="port">端口</param>
        /// <param name="message">消息</param>
        public void BeginSend(string ip, string port, string message)
        {

            IPAddress serverIp = IPAddress.Parse(ip);

            int serverPort = Convert.ToInt32(port);

            IPEndPoint iep = new IPEndPoint(serverIp, serverPort);

            byte[] byteMessage;

            //            do

            //            {

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(iep);



            byteMessage = Encoding.ASCII.GetBytes(message);

            socket.Send(byteMessage);

            socket.Shutdown(SocketShutdown.Both);

            socket.Close();

            //            }

            //            while(byteMessage!=null);

        }
        #endregion
    }
       
}
